package cn.jbolt.xd;

import cn.jbolt.common.model.xd.XdBillAccount;
import cn.jbolt.common.model.xd.XdBillFlows;
import cn.jbolt.common.model.xd.XdBillZixiongWeixin6216;
import com.jfinal.plugin.activerecord.Db;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class XdBillZixiongWeixin6216Utils {

    public static void main(String[] args) {

        XdDBUtils.contect();
        step1();

//        // 读取磁盘上已经存在的Excel文件 传入参数需是对应Excel文件路径的输入流对象
//        String path = "/Users/weizixiong/编程区/流水/wzx_wechat_6216/";
//        String file = "微信支付账单(20250401-20250614).csv";
//        File file1 = new File(path+file);
//        System.out.println(file1.exists());
//        read(path,file);
    }

    private static void read(String path,String file) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 创建 reader
        try (BufferedReader br = Files.newBufferedReader(Paths.get(path+file))) {
            // CSV文件的分隔符
            String DELIMITER = ",";
            // 按行读取
            String line;
            int i = 0;
            while ((line = br.readLine()) != null) {
                i +=1;
                if (i<18){
                    continue;
                }
                // 分割
                String[] columns = line.split(DELIMITER);
                String trade_time = columns[0];
                String trade_type = columns[1];
                String counterparty = columns[2];
                String product = columns[3];
                String trade_mark = columns[4];
                String amount = columns[5].replace("¥","");
                String pay_channel = columns[6];
                String trade_status = columns[7];
                String trade_number = columns[8];
                String shop_number = columns[9];
                String remark = columns[10];

//                if (i == 464){
                if(columns.length >= 13){
                    product = columns[3]+" "+columns[4]+" "+columns[5];
                    trade_mark = columns[6];
                    amount = columns[7].replace("¥","");
                    pay_channel = columns[8];
                    trade_status = columns[9];
                    trade_number = columns[10];
                    shop_number = columns[11];
                    remark = columns[12];
                }else if(columns.length >= 12){
                    product = columns[3]+" "+columns[4];
                    trade_mark = columns[5];
                    amount = columns[6].replace("¥","");
                    pay_channel = columns[7];
                    trade_status = columns[8];
                    trade_number = columns[9];
                    shop_number = columns[10];
                    remark = columns[11];
                }

                trade_type = trade_type.replaceAll("\t|\r|\n", "").replace("\"", "").trim();
                counterparty = counterparty.replaceAll("\t|\r|\n", "").replace("\"", "").trim();
                product = product.replaceAll("\t|\r|\n", "").replace("\"", "").trim();
                trade_mark = trade_mark.replaceAll("\t|\r|\n", "").replace("\"", "").trim();
                pay_channel = pay_channel.replaceAll("\t|\r|\n", "").trim();
                trade_status = trade_status.replaceAll("\t|\r|\n", "").replace("\"", "").trim();
                trade_number = trade_number.replaceAll("\t|\r|\n", "").replace("\"", "").trim();
                shop_number = shop_number.replaceAll("\t|\r|\n", "").replace("\"", "").trim();
                remark = remark.replaceAll("\t|\r|\n", "").replace("\"", "").trim();

                // 打印行
                System.out.println("amount==="+amount);
                System.out.println("User["+ String.join(", ", columns) +"]");

                XdBillZixiongWeixin6216 item = new XdBillZixiongWeixin6216().dao()
                        .findFirst("select * from xd_bill_zixiong_weixin6216 where order_number=?",trade_number);
                if (item==null){
                    item = new XdBillZixiongWeixin6216();
                }
                item.setTradeTime(sdf.parse(trade_time));
                item.setTradeType(trade_type);
                item.setCounterparty(counterparty);
                item.setProduct(product);
                item.setTradeMark(trade_mark);
                item.setAmount(new BigDecimal(amount.trim()));
                item.setPayChannal(pay_channel);
                item.setTradeStatus(trade_status);
                item.setOrderNumber(trade_number);
                item.setShopNumber(shop_number);
                item.setRemark(remark);
                item.setBillFileName(file);
                item.setBillAlias("魏梓雄_微信_6216");
                item.setStatus(0);
                if (item.getTradeType().equals("经营收款")){
                    item.setIsOperatingIncome(true);
                }else{
                    item.setIsOperatingIncome(false);
                }
                if (item.getId()==null){
                    item.save();
                }else{
                    System.out.println(trade_number +"===重复数据");
                    item.update();
                }


            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void getAccount(){
//        List<String> nameList = Db.query("SELECT counterparty from xd_bill_zixiong_weixin6216 WHERE is_operating_income=0 GROUP BY counterparty;");
//        for (String name : nameList){
//            XdBillAccount account = new XdBillAccount().dao().findFirst("select * from xd_bill_account where name=?",name);
//            if (account == null){
//                account = new XdBillAccount();
//                account.setName(name);
//                account.save();
//            }
//        }

//        List<String> nameList = Db.query("SELECT pay_channal from xd_bill_zixiong_weixin6216 WHERE is_operating_income=0 GROUP BY pay_channal;");
//        for (String name : nameList){
//            XdBillAccount account = new XdBillAccount().dao().findFirst("select * from xd_bill_account where alias=?",name);
//            if (account == null){
//                account = new XdBillAccount();
//                account.setName(name.replace("\"",""));
//                account.setAlias(name);
//                account.save();
//            }else{
//                account.setName(name.replace("\"",""));
//                account.update();
//            }
//        }




//        List<XdBillAccount> list = new XdBillAccount().dao().findAll();
//        for (XdBillAccount xdBillAccount : list) {
//            xdBillAccount.setAlias(xdBillAccount.getName());
//            xdBillAccount.update();
//        }
    }

    private static void step1(){
        List<XdBillAccount> accounts = new XdBillAccount().dao().findAll();
        for (XdBillAccount account : accounts) {
            account.setAlias(account.getAlias().replaceAll("\t|\r|\n", "").replace("\"", "").trim());
            account.update();
        }

        List<XdBillFlows> flows = new XdBillFlows().dao().findAll();
        for (XdBillFlows flow : flows) {
            flow.setOutAccount(flow.getOutAccount().replace("\t|\r|\n", "").replace("\"", "").trim());
            flow.setInAccount(flow.getInAccount().replace("\t|\r|\n", "").replace("\"", "").trim());
            flow.update();
        }

    }


//    private static void read1(String filePath) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//
//        try {
//            FileInputStream in = new FileInputStream(filePath);
//            Workbook workbook = WorkbookFactory.create(in);
//            for (int j = 0; j < workbook.getNumberOfSheets(); j++) {
//                // 读取Excel文件中的第一个Sheet页
//                Sheet sheet = workbook.getSheetAt(j);
//                // 获取sheet中最后一行的行号
//                int lastRowNum = sheet.getLastRowNum();
//                for (int i = 17; i <= lastRowNum ; i++) {
//                    // 获得某一行
//                    Row row = sheet.getRow(i);
//                    if (row == null) {
//                        continue;
//                    }
//
//                    for (int k=0; k<row.getPhysicalNumberOfCells(); k++){
//                        row.getCell(k).setCellType(CellType.STRING);
//                        System.out.print(row.getCell(k).getStringCellValue() + ",  ");
//                    }
//                    System.out.println();
//                }
//            }
//
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }



}
